package activity.personal.qianbao;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import adapter.JiaoYiJiLuAdapter;
import base.BaseActivity;
import bean.JiaoYiJiLuBean;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import presenter.SysTemPresenter;
import util.utls.BasePtr;
import util.utls.IsUtilUid;
import util.utls.MyGongJv;
import util.volley.ResultEntity;

/**
 * Created by itboye on 2017/1/6.
 */

public class JiaoYiJiLuActivity extends BaseActivity implements Observer {
    TextView tvTitle;
    ImageView back;
    ListView jiaoyi_list;
    PtrFrameLayout commend_anchor_ptr;
    LinearLayout kong_layoout;
    TextView kong_text;
    JiaoYiJiLuAdapter adapter;

    List<JiaoYiJiLuBean.ListEntity> list;

    SysTemPresenter persenter;
    int pageIndex = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jiao_yi_ji_lu);
        tvTitle.setText(getString(R.string.jiaoyi_jilu));
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新
        persenter = new SysTemPresenter(this);
        showProgressDialog("数据加载中...", true);
        persenter.onZhangDan(IsUtilUid.isUid(), pageIndex + "", "10");
        list = new ArrayList<JiaoYiJiLuBean.ListEntity>();
        adapter = new JiaoYiJiLuAdapter(this, list);
        jiaoyi_list.setAdapter(adapter);
        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                pageIndex++;
                persenter.onZhangDan(IsUtilUid.isUid(), pageIndex + "", "10");
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                persenter.onZhangDan(IsUtilUid.isUid(), pageIndex + "", "10");
            }
        });
        jiaoyi_list.setLayoutAnimation(MyGongJv.getListAnim());
    }


    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;

            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == persenter.dingdan_success) {
                if (pageIndex == 1) {
                    list.clear();
                }
                commend_anchor_ptr.refreshComplete();//设置刷新完成
                JiaoYiJiLuBean bean = (JiaoYiJiLuBean) entity.getData();
                list.addAll(bean.getList());

                if (list.size() > 0) {
                    adapter.notifyDataSetChanged();
                    jiaoyi_list.setVisibility(View.VISIBLE);
                    kong_layoout.setVisibility(View.GONE);

                } else {
                    if (pageIndex == 1) {
                        jiaoyi_list.setVisibility(View.GONE);
                        commend_anchor_ptr.setVisibility(View.GONE);
                        kong_layoout.setVisibility(View.VISIBLE);
                        kong_text.setText("最近没有消费记录");
                    }
                }

            }
            if (entity.getEventType() == persenter.dingdan_fail) {
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
